 //
//  DisCoverViewController.m
//  赵彬旭
//
//  Created by lanou3g on 16/1/15.
//  Copyright © 2016年 赵彬旭. All rights reserved.
//

#import "DisCoverViewController.h"
#import "MJRefresh.h"
#import "DisCoverYouJILayout.h"
#import "DisCoverYouJIManager.h"
#import "DisCoverXuanShangTableViewCell.h"
#import "DisCoverYouJICollectionViewCell.h"
#import "DisCoverDescViewController.h"
#import "UIImageView+WebCache.h"
#import "DisCoverXuanShangDescTableViewController.h"
#import "DisCoverXuanShangSendViewController.h"
//#import "DisCoverAroundViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
//#import "DisCoverAroundViewController.h"
#import "MineTableViewController.h"


#define DISCOVERYOUJI_URL @"http://api.breadtrip.com/v2/index/?next_start=2387098527"

#define YOUJIJIAZAI_URL @"http://api.breadtrip.com/v2/index/?next_start=%@"

#define DISCOVERXUANSHANG_URL @"http://www.qiugonglue.com/api/v3/trends/new_trends_list?client_name=QGLMain&client_version=5.6.53&limit=20&order_by=desc&os_version=21&p=1&platform=android&screen_size=1080x1920&uuid=866769024708664"

#define XUANSHANG_URL @"http://www.qiugonglue.com/api/v3/trends/new_trends_list?client_name=QGLMain&client_version=5.6.53&limit=20&order_by=desc&os_version=21&p=%ld&platform=android&screen_size=1080x1920&uuid=866769024708664"

#define DARENXIANGQING_URL @"http://www.qiugonglue.com/api/v3/trends/new_trends_list?client_name=QGLMain&client_version=5.6.53&limit=20&order_by=asc&os_version=21&p=1&platform=android&screen_size=1080x1920&user_id=%@"






#define kWidth self.view.bounds.size.width

#define kHeight self.view.bounds.size.height

@interface DisCoverViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,DisCoverYouJILayouDelegate,UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,CLLocationManagerDelegate>

@property(nonatomic,strong)NSMutableArray * dataArray;

@property(nonatomic,strong)NSArray *disyoujiSegmentArray;

@property(nonatomic,strong)UISegmentedControl * disyoujiSegment;

//@property(nonatomic,strong)UITableView * disyoujiTableView;

@property(nonatomic,strong)NSMutableArray * soureArray;

@property(nonatomic,strong)UICollectionView *youjicollectionView;

@property(nonatomic,strong)UITableView * xuanshangTableView;

@property(nonatomic,assign)BOOL hasLoad;

@property(nonatomic,strong)NSMutableArray * allDataArray;

@property(nonatomic,strong)NSMutableArray * allSoureArray;



#pragma mark  --- 以下是悬赏  --

@property(nonatomic,strong)NSMutableArray * xuanshangDataArray;

@property(nonatomic,strong)NSMutableArray * xuanshangSourceArray;

@property(nonatomic,strong)NSMutableArray * allxuanshangDataArray;

@property(nonatomic,strong)NSMutableArray * allxuanshangSourceArray;

@property(nonatomic,assign)int i;



#pragma mark  --- 以下是附近 --

@property(nonatomic,strong)UIButton * aroundButton;

@property(nonatomic,strong)UIButton *buttonOne;

@property(nonatomic,strong)UILabel * labelOne;

@property(nonatomic,strong)UIButton *buttonTwo;

@property(nonatomic,strong)UILabel * labelTwo;

@property(nonatomic,strong)UIButton *buttonThree;

@property(nonatomic,strong)UILabel * labelThree;

@property(nonatomic,strong)UIButton *buttonFour;

@property(nonatomic,strong)UILabel * labelFour;

@property(nonatomic,strong)UIButton * buttonFive;

@property(nonatomic,strong)UILabel * labelFive;

@property(nonatomic,strong)UIButton * buttonSix;

@property(nonatomic,strong)UILabel * labelSix;

@property(nonatomic,strong)UIButton * buttonSeven;

@property(nonatomic,strong)UILabel * labelSeven;

@property(nonatomic,strong)UIButton * buttonEight;

@property(nonatomic,strong)UILabel * labelEight;




@property(nonatomic,strong)NSMutableArray *xiabiaoArray;







@property(nonatomic,strong)CLLocationManager * locationManager;

@property(nonatomic,strong)NSString * dd;

@property(nonatomic,strong)NSString * cc;

@property(nonatomic,assign)BOOL isContains;


@property(nonatomic,strong)NSMutableArray * quickSortWithArray;

@property(nonatomic,strong)UIView *aaView;

@property(nonatomic,strong)NSMutableDictionary *dictionary;


@end

@implementation DisCoverViewController

NSString *identifier = @"cell";

NSString *xuanshangIdentifier = @"xuanshangcell";


- (void)viewDidLoad {
    
    [super viewDidLoad];
 
    //self.navigationController.navigationBar.translucent = YES;
    
    self.disyoujiSegmentArray = @[@"游记",@"附近",@"悬赏"];
    
    self.disyoujiSegment = [[UISegmentedControl alloc] initWithItems:_disyoujiSegmentArray];
    
     _disyoujiSegment.selectedSegmentIndex = 0;
    
    _disyoujiSegment.frame = CGRectMake(0, 0, self.view.frame.size.width, 30);
    
    self.navigationController.navigationBar.translucent = NO;
    
    [_disyoujiSegment addTarget:self action:@selector(segmentChange:) forControlEvents:UIControlEventValueChanged];
    
    self.dataArray = [NSMutableArray array];
    
    self.soureArray = [NSMutableArray array];
    
    self.allDataArray = [NSMutableArray array];
    
    self.allSoureArray = [NSMutableArray array];
    
    self.xuanshangDataArray = [NSMutableArray array];
    
    self.xuanshangSourceArray = [NSMutableArray array];
    
    self.allxuanshangDataArray = [NSMutableArray array];
    
    self.allxuanshangSourceArray = [NSMutableArray array];
    
    self.i = 2;
    
    
    _xuanshangTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width,self.view.frame.size.height -49-30 ) style:UITableViewStylePlain];
    
    _xuanshangTableView.backgroundColor = [UIColor whiteColor];
    
    _xuanshangTableView.delegate = self;
    
    _xuanshangTableView.dataSource = self;
    
    
    
    
    
    [self segmentChange:_disyoujiSegment];
    
    
    [self setRefresh];
    
    
    [self.view addSubview:_disyoujiSegment];
    
    
    [DisCoverYouJIManager shareDisCoverYouJIManager].upDataUI = ^(){
    
        _dataArray = [DisCoverYouJIManager shareDisCoverYouJIManager].dataArray;
        
        _soureArray = [DisCoverYouJIManager shareDisCoverYouJIManager].numberArray;
    
        if ([_allSoureArray containsObject:_soureArray[0]]&&_xuanshangSourceArray.count!= 0) {
            
            [_soureArray removeAllObjects];
            
            [_dataArray removeAllObjects];

            
        }else{
            
            [_allSoureArray addObjectsFromArray:_soureArray];
            
            [_allDataArray addObjectsFromArray:_dataArray];
            
        }
    
        
        dispatch_async(dispatch_get_main_queue(), ^{
        
                 [self.youjicollectionView reloadData];
            
        });
     
    };
    
    
    [DisCoverYouJIManager shareDisCoverYouJIManager].xuanshangUpDataUI= ^(){
        
        
        _xuanshangDataArray = [DisCoverYouJIManager shareDisCoverYouJIManager].xuanshangDataArray;
        
        _xuanshangSourceArray = [DisCoverYouJIManager shareDisCoverYouJIManager].xuanshangNumberArray;
        
        
        NSLog(@"xuanshang count %ld",_xuanshangSourceArray.count);
        
        
        
        if ([_allxuanshangSourceArray containsObject:_xuanshangSourceArray[0]]&&_xuanshangSourceArray.count != 0) {
            
            _xuanshangSourceArray = [NSMutableArray array];
            
            [_xuanshangDataArray removeAllObjects];
            
            
        }else{
            
            [_allxuanshangSourceArray addObjectsFromArray:_xuanshangSourceArray];
            
            [_allxuanshangDataArray addObjectsFromArray:_xuanshangDataArray];
            
        }
        
        
        NSLog(@"XXXX%ld",_allxuanshangSourceArray.count);
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
      
                [self.xuanshangTableView reloadData];
                
        });
 
    };
    
    
    
    
    
    
      [_youjicollectionView registerClass:[DisCoverYouJICollectionViewCell class] forCellWithReuseIdentifier:identifier];
    
    
    [_xuanshangTableView registerNib:[UINib nibWithNibName:@"DisCoverXuanShangTableViewCell" bundle:nil] forCellReuseIdentifier:xuanshangIdentifier];
    

    
    
    if (_xiangqingID != nil) {
        
        
        [_disyoujiSegment removeFromSuperview];
        
        self.title = [NSString stringWithFormat:@"%@的动态",_navName];
        
        _xuanshangTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width,self.view.frame.size.height -49-30 ) style:UITableViewStylePlain];
        
        _xuanshangTableView.backgroundColor = [UIColor whiteColor];
        
        _xuanshangTableView.delegate = self;
        
        _xuanshangTableView.dataSource = self;
        
        
       // NSLog(@"00000000============000------%@",_xiangqingID);
        
        [[DisCoverYouJIManager shareDisCoverYouJIManager]requestXuanShangNetWork:[NSString stringWithFormat:DARENXIANGQING_URL,_xiangqingID]];
        
       // [self setRefresh];
        
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(marray:)];
        
      
        _xiangqingID = 0;
        
        [self.view addSubview:_xuanshangTableView];
     
       
    }
    
}



-(void)marray:(UIBarButtonItem *)sender{

    [_allxuanshangSourceArray removeAllObjects];
    
    [self.navigationController popViewControllerAnimated:YES];

}






-(CGFloat)heightForItemIndexPath:(NSIndexPath *)indexPath{
    

    CGFloat h =arc4random()%(180-110+1)+150;
    
    return h;
 
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    
    return _allDataArray.count;
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  
    
    if (!_hasLoad) {
        
        [collectionView registerClass:[DisCoverYouJICollectionViewCell class] forCellWithReuseIdentifier:identifier];
        
        _hasLoad = YES;
        
   }
    
         DisCoverYouJICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        
        cell.model = _allDataArray[indexPath.item];
 
       _hasLoad = NO;
    
        return cell;
    
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    DisCoverDescViewController *dis = [[DisCoverDescViewController alloc] init];
    
    dis.string = _allSoureArray[indexPath.item][@"data"][0][@"id"];


    [self.navigationController pushViewController:dis animated:YES];


}


-(void)segmentChange:(UISegmentedControl *)sender{

    if (sender.selectedSegmentIndex == 0) {
        
       [_xuanshangTableView removeFromSuperview];
        
        self.navigationItem.rightBarButtonItem = nil;
        
        DisCoverYouJILayout *flowLayout = [[DisCoverYouJILayout alloc] init];
        
        //[self setRefresh];
        
        flowLayout.delegate = self;
        
        CGFloat w = ([UIScreen mainScreen].bounds.size.width - 30)/3;
        
        flowLayout.itemSize = CGSizeMake(w, w);
        
        flowLayout.insertItemSpacing = 5;
        
        flowLayout.sectionInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        
        flowLayout.numberOfColumn = 3; //列数
        
        self.youjicollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width , self.view.frame.size.height-44-29) collectionViewLayout:flowLayout];
        
        _youjicollectionView.delegate = self;
        
        _youjicollectionView.dataSource = self;
        
        
        self.view.backgroundColor = [UIColor whiteColor];
  
        
        _youjicollectionView.backgroundColor = [UIColor whiteColor];
        
       [[DisCoverYouJIManager shareDisCoverYouJIManager]requestNetWork:DISCOVERYOUJI_URL];
        
        [self setRefresh];
        
        
        [self.view addSubview:_youjicollectionView];
        
        
    }else if (sender.selectedSegmentIndex == 1){
        
        
      //  [_allDataArray removeAllObjects];
        
      //  [_allSoureArray removeAllObjects];
        
       //[_allxuanshangSourceArray removeAllObjects];
        
        //[_allxuanshangDataArray removeAllObjects];
        
        
        
        [_youjicollectionView removeFromSuperview];
        
       // [_aaView removeFromSuperview];
        
        
        self.aaView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 30)];
        
        _aaView.backgroundColor = [UIColor whiteColor];
        
        
        
        
        self.navigationItem.rightBarButtonItem = nil;
        
        
        [_xuanshangTableView removeFromSuperview];
        
       
        self.view.backgroundColor = [UIColor whiteColor];
        
        
        self.aroundButton = [UIButton buttonWithType:UIButtonTypeSystem];
        
        
        _aroundButton.frame = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2 , 200, 200);
        
        _aroundButton.titleLabel.font = [UIFont systemFontOfSize:40];
        
        _aroundButton.layer.cornerRadius = 100;
        
        _aroundButton.layer.masksToBounds = YES;
        
        
        _aroundButton.center = self.view.center;
        
        [_aroundButton setTitle:@"附近的人" forState:UIControlStateNormal];
        
        [_aroundButton addTarget:self action:@selector(touchbutton:) forControlEvents:UIControlEventTouchUpInside];
        
        
        _aroundButton.backgroundColor = [UIColor greenColor];
        
        
        [_aaView addSubview:_aroundButton];
        
        
        UIButton *buttonOne = [[UIButton alloc] initWithFrame:CGRectMake(10, 40, 90, 80)];
        
        UILabel *lableOne = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 90, 20)];
        
        buttonOne.backgroundColor = [UIColor redColor];
        
        lableOne.backgroundColor = [UIColor orangeColor];
        
        [buttonOne addSubview:lableOne];
        
        self.labelOne = lableOne;
        
        self.buttonOne = buttonOne;
        
        [_aaView addSubview:_buttonOne];
        
        
        UIButton *buttonTwo = [[UIButton alloc] initWithFrame:CGRectMake(110, 40, 90, 80)];
        
        UILabel *lableTwo = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 90, 20)];
        
        buttonTwo.backgroundColor = [UIColor redColor];
        
        lableTwo.backgroundColor = [UIColor orangeColor];
        
        [buttonTwo addSubview:lableTwo];
        
        self.labelTwo = lableTwo;
        
        self.buttonTwo = buttonTwo;
        
        [_aaView addSubview:_buttonTwo];
        
        
        UIButton *buttonThree = [[UIButton alloc] initWithFrame:CGRectMake(210, 40, 90, 80)];
        
        UILabel *lableThree = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 90, 20)];
        
        buttonThree.backgroundColor = [UIColor redColor];
        
        lableThree.backgroundColor = [UIColor orangeColor];
        
        [buttonThree addSubview:lableThree];
        
        self.labelThree = lableThree;
        
        self.buttonThree = buttonThree;
        
        
        [_aaView addSubview:_buttonThree];
        
        
        
        UIButton *buttonFour = [[UIButton alloc] initWithFrame:CGRectMake(310, 40, 90, 80)];
        
        UILabel *lableFour = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 90, 20)];
        
        buttonFour.backgroundColor = [UIColor redColor];
        
        lableFour.backgroundColor = [UIColor orangeColor];
        
        [buttonFour addSubview:lableFour];
        
        self.labelFour = lableFour;
        
        self.buttonFour = buttonFour;
        
        [_aaView addSubview:_buttonFour];
        
        
        self.buttonFive = [[UIButton alloc] initWithFrame:CGRectMake(10, 140, 90, 80)];
        
        self.labelFive = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 90, 20)];
        
        _buttonFive.backgroundColor = [UIColor redColor];
        
        _labelFive.backgroundColor = [UIColor orangeColor];
        
        [_buttonFive addSubview:_labelFive];
        
        [_aaView addSubview:_buttonFive];
        
        
        self.buttonSix = [[UIButton alloc] initWithFrame:CGRectMake(110, 140, 90, 80)];
        
        self.labelSix = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 90, 20)];
        
        _buttonSix.backgroundColor = [UIColor redColor];
        
        _labelSix.backgroundColor = [UIColor orangeColor];
        
        [_buttonSix addSubview:_labelSix];
        
        [_aaView addSubview:_buttonSix];
        
        
        
        self.buttonSeven = [[UIButton alloc] initWithFrame:CGRectMake(210, 140, 90, 80)];
        
        self.labelSeven= [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 90, 20)];
        
        _buttonSeven.backgroundColor = [UIColor redColor];
        
        _labelSeven.backgroundColor = [UIColor orangeColor];
        
        [_buttonSeven addSubview:_labelSeven];
        
        [_aaView addSubview:_buttonSeven];
        
        
        
        self.buttonEight = [[UIButton alloc] initWithFrame:CGRectMake(310, 140, 90, 80)];
        
        self.labelEight = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 90, 20)];
        
        _buttonEight.backgroundColor = [UIColor redColor];
        
        _labelEight.backgroundColor = [UIColor orangeColor];
        
        [_buttonEight addSubview:_labelEight];
        
        [_aaView addSubview:_buttonEight];
        
        
        
        [self.view addSubview:_aaView];
  
        
    }else if (sender.selectedSegmentIndex == 2){
        
       self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc] initWithTitle:@"发起悬赏" style:UIBarButtonItemStylePlain target:self action:@selector(fasongxuanshang:)];
        
        _xuanshangTableView.delegate =self;
        
        _xuanshangTableView.dataSource = self;
        
       // _allxuanshangDataArray = [NSMutableArray array];
        
        //_allxuanshangSourceArray = [NSMutableArray array];
        
        
      // [_youjicollectionView removeFromSuperview];
        
        //self.allxuanshangDataArray = [NSMutableArray array];
        
        //self.allxuanshangSourceArray = [NSMutableArray array];
        

        //[self setRefresh];
        
       // _allSoureArray = [NSMutableArray array];
        
       
        
           [[DisCoverYouJIManager shareDisCoverYouJIManager]requestXuanShangNetWork:DISCOVERXUANSHANG_URL];
        
        [self setRefresh];
        
        
      
        
        [self.view addSubview:_xuanshangTableView];
        
     
    }

}

//-(void)viewWillAppear:(BOOL)animated{
//
//dispatch_async(dispatch_get_main_queue(), ^{
//    [_xuanshangTableView reloadData];
//});


//}








-(void)touchbutton:(UIButton *)sender{  //附近的人位置排列
    
    [self initializeLocationService];
    
//    CGFloat x = arc4random()%(NSInteger)(kWidth - 100);
//    
//    CGFloat y = arc4random()%(NSInteger)(kHeight - 100);
    

    self.dictionary = [NSMutableDictionary dictionary];
    
    if (_allxuanshangSourceArray.count == 0) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请先浏览" message:@"待小拍慢慢搜索" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
        
        [alertView show];
        
    }else{
    
    // NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 0; i<_allxuanshangSourceArray.count; i++) {
        
        float  lat = [_allxuanshangSourceArray[i][@"lat"] floatValue];
        
        float lon =  [_allxuanshangSourceArray[i][@"lon"] floatValue];
        
       // NSLog(@"00000---=======%ld",_allxuanshangSourceArray.count);
        
        
       CGFloat dis = [self countDistance:lat And: lon];
        
        
//        NSString *str = [NSString stringWithFormat:@"%lf",dis];
        
       // NSNumber *number = [NSNumber numberWithInteger:dis];
        
        NSString *strr = [NSString stringWithFormat:@"%lf",dis];
        
        
       // [array addObject:strr];
        
        
        [_dictionary setValue:strr forKey:[NSString stringWithFormat:@"%d",i]];
        
       // NSLog(@"\\\\\%@",_dictionary);
        
        
        }
    
    
  //  [self quickSortWithArray:array];
    
    [self selectSortWithDictionary:_dictionary];
        
    }
    
    }
    




-(void)selectSortWithDictionary:(NSMutableDictionary *)aDictionary{ //选择排序
    
    //NSMutableArray *longArray = [NSMutableArray array];
    
     NSMutableArray *data = [NSMutableArray array] ;
    
    NSMutableArray *paixuhouArray = [NSMutableArray array];

     NSMutableArray *showArray = [NSMutableArray array];
    
    self.xiabiaoArray = [NSMutableArray array];
    
    for (int i = 0; i<_allxuanshangSourceArray.count; i++) {

        NSString * key = [NSString stringWithFormat:@"%d",i];
        
       NSString * aData = aDictionary[key];

        [data addObject:aData];
       
    }
  
    for (int i=0; i<data.count-1; i++) {
        
           int m = i;
        
            for (int j =i+1; j<[data count]; j++) {
                
                if ([[data objectAtIndex:j] floatValue] < [[data objectAtIndex:m] floatValue]) {
                               m = j;
                           }
                 }
        
             if (m != i) {
                 
           paixuhouArray = [self swapWithData:data index1:m index2:i];
                 
                 
              }
        
         }
    
       // NSLog(@"==========paixuhou后 %@",paixuhouArray);
        
       // [longArray addObjectsFromArray:[data description]];
        
    // NSLog(@"选择排序后的结果：%@ %@ data %@",[data description],key,data);
    
    
         for (int j = 0; j< paixuhouArray.count; j++) {

    for (int i = 0; i< paixuhouArray.count; i++) {
        
        NSString * keykey = [NSString stringWithFormat:@"%d",i];
        
    
            if ([aDictionary[keykey] isEqualToString:paixuhouArray[j]] && [aDictionary[keykey] floatValue]!= 0) {
                
        
                    if ( [showArray containsObject:(id)_allxuanshangSourceArray[j][@"author"][@"user_id"] ]) {
                        
                       // NSLog(@"----=--=-=-=-=-=%@",_allxuanshangSourceArray[j][@"author"][@"user_id"]);
                      
                    }else{
                       
                        [showArray addObject:_allxuanshangSourceArray[j][@"author"][@"user_id"]];
                        
                        // NSLog(@"%@",_allxuanshangSourceArray[j][@"author"][@"user_id"]);
                        
                       // NSLog(@"333TT %@",showArray);
                        
                
                        
                        if ([_xiabiaoArray containsObject:(id)keykey]) {
                            
                           // NSLog(@"jjjjjjjjjjjjjjjjjjjjjjjjjjjj");
                            
                        }else{
                        
                        [_xiabiaoArray addObject:keykey];
                        
                       // NSLog(@"xiabiao %@",xiabiaoArray);
                            
                        }
                        
                    }
  
            }
    }
         }
    
    
    
    
    
    
    
    
   // NSLog(@"000  --%@",xiabiaoArray);

    
    NSString * ssss = _xiabiaoArray[0];
    
    NSString * dddd = _xiabiaoArray[1];
    
    NSString * wwww = _xiabiaoArray[2];
    
    NSString * qqqq = _xiabiaoArray[3];
    
    NSString * aaaa = _xiabiaoArray[4];
    
    NSString * eeee = _xiabiaoArray[5];
    
    NSString * zzzz = _xiabiaoArray[6];
    
    NSString * cccc = _xiabiaoArray[7];
    
    
    
    
    _labelOne.text = [NSString stringWithFormat:@"%1d 千米",[_dictionary[ssss] intValue]/1000] ;
    
    NSString * buttonOneStr = _allxuanshangSourceArray[[ssss intValue]][@"author"][@"avatar"];
    
    
    _labelTwo.text = [NSString stringWithFormat:@"%1d千米",[_dictionary[dddd] intValue]/1000];

    NSString * buttonTwoStr = _allxuanshangSourceArray[[dddd intValue]][@"author"][@"avatar"];
    
    
    _labelThree.text = [NSString stringWithFormat:@"%1d千米",[_dictionary[wwww] intValue]/1000];
    
    
    NSString * buttonThreeStr = _allxuanshangSourceArray[[wwww intValue]][@"author"][@"avatar"];
    
    
    _labelFour.text = [NSString stringWithFormat:@"%1d千米",[_dictionary[qqqq] intValue]/1000];
    
    
    NSString * buttonFourStr = _allxuanshangSourceArray[[qqqq intValue]][@"author"][@"avatar"];
    
    
    _labelFour.text = [NSString stringWithFormat:@"%1d千米",[_dictionary[qqqq] intValue]/1000];
    
    
    NSString * buttonFiveStr = _allxuanshangSourceArray[[aaaa intValue]][@"author"][@"avatar"];
    
    _labelFive.text = [NSString stringWithFormat:@"%1d千米",[_dictionary[aaaa] intValue]/1000];
    
    
    NSString * buttonSixStr = _allxuanshangSourceArray[[eeee intValue]][@"author"][@"avatar"];
    
    _labelSix.text = [NSString stringWithFormat:@"%1d千米",[_dictionary[eeee] intValue]/1000];
    
    
    NSString * buttonSevenStr = _allxuanshangSourceArray[[zzzz intValue]][@"author"][@"avatar"];
    
    _labelSeven.text = [NSString stringWithFormat:@"%1d千米",[_dictionary[zzzz] intValue]/1000];
    
    
    
    
    NSString * buttonEigntStr = _allxuanshangSourceArray[[cccc intValue]][@"author"][@"avatar"];
    
    _labelEight.text = [NSString stringWithFormat:@"%1d千米",[_dictionary[cccc] intValue]/1000];
    
    
    
    
    
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    
    dispatch_async(queue, ^{
        
//        [_buttonOne setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonOneStr]]] forState:UIControlStateNormal];
       
        
//        _buttonOne.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonOneStr]]]];
        
        
        UIImage *buttonImage = [UIImage  imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonOneStr]]];
        UIImage *stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:12  topCapHeight:0];
        [_buttonOne  setBackgroundImage:stretchableButtonImage  forState:UIControlStateNormal];
        
        
        [_buttonOne addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
       
        
        
    });
        
     dispatch_async(queue, ^{
        
//        [_buttonTwo setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonTwoStr]]] forState:UIControlStateNormal];
         
//         _buttonTwo.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonTwoStr]]]];
         
         
         
         UIImage *buttonImage = [UIImage  imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonTwoStr]]];
         UIImage *stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:12  topCapHeight:0];
         [_buttonTwo  setBackgroundImage:stretchableButtonImage  forState:UIControlStateNormal];
         
         [_buttonTwo addTarget:self action:@selector(buttonActionTwo:) forControlEvents:UIControlEventTouchUpInside];
         
         });
         
        dispatch_async(queue, ^{
         
//        [_buttonThree setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonThreeStr]]] forState:UIControlStateNormal];
            
//            _buttonThree.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonThreeStr]]]];
            
            
            
            UIImage *buttonImage = [UIImage  imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonThreeStr]]];
            UIImage *stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:12  topCapHeight:0];
            [_buttonThree  setBackgroundImage:stretchableButtonImage  forState:UIControlStateNormal];
            
            
            [_buttonThree addTarget:self action:@selector(buttonActionThree:) forControlEvents:UIControlEventTouchUpInside];
            
            
            });
            
            
            dispatch_async(queue, ^{
            
//        [_buttonFour setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonFourStr]]] forState:UIControlStateNormal];
                
//                _buttonFour.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonFourStr]]]];
                
                
                UIImage *buttonImage = [UIImage  imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonFourStr]]];
                UIImage *stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:12  topCapHeight:0];
                [_buttonFour  setBackgroundImage:stretchableButtonImage  forState:UIControlStateNormal];
                
                
           [_buttonFour addTarget:self action:@selector(buttonActionFour:) forControlEvents:UIControlEventTouchUpInside];
                
        
    });
    
    
    dispatch_async(queue, ^{
        
//        [_buttonFive setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonFiveStr]]] forState:UIControlStateNormal];
        
        
        
//        _buttonFive.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonFiveStr]]]];
        
        
        UIImage *buttonImage = [UIImage  imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonFiveStr]]];
        UIImage *stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:12  topCapHeight:0];
        [_buttonFive setBackgroundImage:stretchableButtonImage  forState:UIControlStateNormal];
        
        [_buttonFive addTarget:self action:@selector(buttonActionFive:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
    });
    
    
    dispatch_async(queue, ^{
        
//        [_buttonSix setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonSixStr]]] forState:UIControlStateNormal];
        
//        _buttonSix.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonSixStr]]]];
        
        
        UIImage *buttonImage = [UIImage  imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonSixStr]]];
        UIImage *stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:12  topCapHeight:0];
        [_buttonSix  setBackgroundImage:stretchableButtonImage  forState:UIControlStateNormal];
        
        
        [_buttonSix addTarget:self action:@selector(buttonActionSix:) forControlEvents:UIControlEventTouchUpInside];
        
        
    });
    
    
    dispatch_async(queue, ^{
        
//        [_buttonSeven setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonSevenStr]]] forState:UIControlStateNormal];
        
//        _buttonSeven.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonSevenStr]]]];
        
        UIImage *buttonImage = [UIImage  imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonSevenStr]]];
        UIImage *stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:12  topCapHeight:0];
        [_buttonSeven  setBackgroundImage:stretchableButtonImage  forState:UIControlStateNormal];
        
        
        
        
        [_buttonSeven addTarget:self action:@selector(buttonActionSeven:) forControlEvents:UIControlEventTouchUpInside];
        
        
    });
    
    
    dispatch_async(queue, ^{
        
//        [_buttonEight setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonEigntStr]]] forState:UIControlStateNormal];
        
       // _buttonEight.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonEigntStr]]]];
        //_buttonEight.imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        UIImage *buttonImage = [UIImage  imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:buttonEigntStr]]];
        UIImage *stretchableButtonImage = [buttonImage  stretchableImageWithLeftCapWidth:12  topCapHeight:0];
        [_buttonEight  setBackgroundImage:stretchableButtonImage  forState:UIControlStateNormal];
        
        
        [_buttonEight addTarget:self action:@selector(buttonActionEight:) forControlEvents:UIControlEventTouchUpInside];
        
        
    });
   
    
    
    
    
    
    
}




-(void)buttonAction:(UIButton *)sender{

 [self didButtonAndindex:0];

}

-(void)buttonActionTwo:(UIButton *)sender{
    
    [self didButtonAndindex:1];
    
}

-(void)buttonActionThree:(UIButton *)sender{
    
    [self didButtonAndindex:2];
    
}

-(void)buttonActionFour:(UIButton *)sender{
    
    [self didButtonAndindex:3];
    
}

-(void)buttonActionFive:(UIButton *)sender{
    
    [self didButtonAndindex:4];
    
}

-(void)buttonActionSix:(UIButton *)sender{
    
    [self didButtonAndindex:5];
    
}

-(void)buttonActionSeven:(UIButton *)sender{
    
    [self didButtonAndindex:6];
    
}

-(void)buttonActionEight:(UIButton *)sender{
    
    [self didButtonAndindex:7];
    
}







-(void)didButtonAndindex:(NSInteger)index{

    MineTableViewController * mine = [[MineTableViewController alloc] init];
    
    mine.setrr = @"remm";
    
    mine.headViewString = _allxuanshangSourceArray[[_xiabiaoArray[index] intValue]][@"author"][@"avatar"];
    
    mine.nameLabelStr = _allxuanshangSourceArray[[_xiabiaoArray[index] intValue]][@"author"][@"user_name"];
    
    mine.idLabelStr = _allxuanshangSourceArray[[_xiabiaoArray[index] intValue]][@"author"][@"user_id"];
    
   // mine.indexText = index;
    
   // mine.idText = _allxuanshangSourceArray[[_xiabiaoArray[index] intValue]][@"author"][@"user_id"];
    
    [self.navigationController pushViewController:mine animated:YES];

    
    

}











 -(NSMutableArray *)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1 index2:(NSInteger)index2{
     
     NSNumber *tmp = [aData objectAtIndex:index1];
     
     [aData replaceObjectAtIndex:index1 withObject:[aData objectAtIndex:index2]];
     
     [aData replaceObjectAtIndex:index2 withObject:tmp];
     
     return aData;
     
 }






















//计算两个位置之间的距离
-(CLLocationDistance)countDistance:(CGFloat)lat And:(CGFloat)lon
{
    //根据经纬度创建两个位置对象
    CLLocation *loc1=[[CLLocation alloc]initWithLatitude:[_cc floatValue]longitude:[_dd floatValue]];
    CLLocation *loc2=[[CLLocation alloc]initWithLatitude:lat longitude:lon];
    NSLog(@"lat -- %lf",lat);
    NSLog(@"lon -- %lf",lon);
    
    //计算两个位置之间的距离
    CLLocationDistance distance=[loc1 distanceFromLocation:loc2];
    
    NSLog(@"(%@)和(%@)的距离=%fM",loc1,loc2,distance);
    
    return distance;
    
}




- (void)initializeLocationService {
    // 初始化定位管理器
    _locationManager = [[CLLocationManager alloc] init];
    // 设置代理
    _locationManager.delegate = self;
    // 设置定位精确度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 设置过滤器为无
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 开始定位
    // 取得定位权限，有两个方法，取决于你的定位使用情况
    // 一个是requestAlwaysAuthorization，一个是requestWhenInUseAuthorization
    [_locationManager requestAlwaysAuthorization];//这句话ios8以上版本使用。
    [_locationManager startUpdatingLocation];
    
}




-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    
    self.dd = [NSString stringWithFormat:@"%lf",newLocation.coordinate.longitude];
    
    NSLog(@"dd = %@",_dd);
    
    self.cc = [NSString stringWithFormat:@"%lf",newLocation.coordinate.latitude];
    
    NSLog(@"cc = %@",_cc);
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (placemarks.count > 0) {
            
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            
            NSString *city = placemark.locality;
            
            if (!city) {
                
                city = placemark.administrativeArea;
                
            }
            
            NSLog(@"city = %@",city);
            
            NSString *city4 = city;
            
            NSLog(@"city4 == %@",city4);
            
            
        }else if (error == nil && [placemarks count] == 0){
            
            NSLog(@"00000");
            
            
        }else if (error != nil){
            
            NSLog(@"11111");
            
        }
    }];
    
    [manager stopUpdatingLocation];
    
    
    
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (!_hasLoad) {
        
        UINib *nib = [UINib nibWithNibName:@"DisCoverXuanShangTableViewCell" bundle:nil];
        
        [tableView registerNib:nib forCellReuseIdentifier:xuanshangIdentifier];
        
        _hasLoad = YES;
        
    }
    
     DisCoverXuanShangTableViewCell *xuanshangcell = [tableView dequeueReusableCellWithIdentifier:xuanshangIdentifier];
  
    NSMutableDictionary *diac= [_allxuanshangDataArray[indexPath.row] author];
    xuanshangcell.nameLabel.text = diac[@"user_name"];
 
   xuanshangcell.placeLabel.text = [_allxuanshangDataArray[indexPath.row] place_name];
    
    xuanshangcell.titileLabel.numberOfLines = 0;
    xuanshangcell.titileLabel.text = [_allxuanshangDataArray[indexPath.row] content];
    
    [xuanshangcell.pinglunButton addTarget:self action:@selector(pinglunButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    xuanshangcell.igView.contentMode =  UIViewContentModeScaleAspectFill;

    xuanshangcell.igView.layer.masksToBounds = YES;
    
   [xuanshangcell.igView sd_setImageWithURL:[NSURL URLWithString:[_allxuanshangDataArray[indexPath.row] cover_image]] placeholderImage:nil];
    
    
    _hasLoad = NO;
    
    return xuanshangcell;
    
}


-(void)pinglunButtonAction:(UIButton *)sender{
    
    NSLog(@"模态登陆");
    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSLog(@"QQQQQQQQQQQQQQ%ld",_allxuanshangSourceArray.count);
    
   
    
    
    
    return _allxuanshangDataArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 400;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DisCoverXuanShangDescTableViewController *disc = [[DisCoverXuanShangDescTableViewController alloc] init];
    
    disc.nameImgViewStr = _allxuanshangSourceArray[indexPath.row][@"author"][@"avatar"];
    
    disc.nameLabelStr =_allxuanshangSourceArray[indexPath.row][@"author"][@"user_name"];
    
    disc.titileLabelStr = _allxuanshangSourceArray[indexPath.row][@"content"];
    
    disc.placeLabelStr = _allxuanshangSourceArray[indexPath.row][@"geoinfo"];
    
    disc.bigImgViewStr =_allxuanshangSourceArray[indexPath.row][@"cover_image"];
    
    disc.timeLabelStr = _allxuanshangSourceArray[indexPath.row][@"human_ctime"];
    

   
    [self.navigationController pushViewController:disc animated:YES];
 
}




-(void)fasongxuanshang:(UIBarButtonItem *)sender{

    NSLog(@"发起悬赏");
    
    DisCoverXuanShangSendViewController *sendxuanshang = [[DisCoverXuanShangSendViewController alloc] init];
    
    [self.navigationController pushViewController:sendxuanshang animated:YES];
    
    

}







- (void)setRefresh{
    
    // 下拉刷新:创建该控件有两种方式：block方法、target-action方式
    
    
    
     if (_disyoujiSegment.selectedSegmentIndex == 0) {
    
         self.youjicollectionView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
             
             // 模拟延迟加载数据，因此1秒后才调用。开发中，在这里执行网络请求的任务，当任务执行完毕，调用结束刷新的代码停止刷新
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 
                 
                 [[DisCoverYouJIManager shareDisCoverYouJIManager]requestXuanShangNetWork:DISCOVERXUANSHANG_URL];
                 
                 // 结束刷新
                 [self.youjicollectionView.mj_header endRefreshing];
                 
             });
         }];
    
    
         
            }else if (_disyoujiSegment.selectedSegmentIndex == 1){
            
            
     
                
                
                
                
            }else if (_disyoujiSegment.selectedSegmentIndex == 2){
                
                
                
                self.xuanshangTableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                    
                    // 模拟延迟加载数据，因此2秒后才调用。开发中，在这里执行网络请求的任务，当任务执行完毕，调用结束刷新的代码停止刷新
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        
                        
                     [[DisCoverYouJIManager shareDisCoverYouJIManager]requestXuanShangNetWork:DISCOVERXUANSHANG_URL];
                        
                        // 结束刷新
                        [self.xuanshangTableView.mj_header endRefreshing];
                        
                    });
                }];
                
                
          
            }
            
    
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    self.youjicollectionView.mj_header.automaticallyChangeAlpha = YES;
    self.xuanshangTableView.mj_header.automaticallyChangeAlpha = YES;
    
    
    // 自定义控件样式
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    //    // 设置菊花样式
    footer.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    //    // 闲置状态
    [footer setTitle:@"向上拽，加载新内容" forState:MJRefreshStateIdle];
    //    // 正在加载状态
    [footer setTitle:@"正在为你加载数据" forState:MJRefreshStateRefreshing];
    //    // 无法加载更多的状态

    if (_disyoujiSegment.selectedSegmentIndex == 0){
    
    
    self.youjicollectionView.mj_footer = footer;
        
    }else if (_disyoujiSegment.selectedSegmentIndex ==1){
    
        
        
        
    
    }else if (_disyoujiSegment.selectedSegmentIndex == 2){
    
        self.xuanshangTableView.mj_footer = footer;
        
    }
    
    
    
    
    
}





- (void)loadData {
    
    // NSLog(@"加载数据");
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
        if (_disyoujiSegment.selectedSegmentIndex == 0) {
            
            NSString*dd = _allSoureArray.lastObject[@"data"][0][@"id"];
            
            NSLog(@"count = %ld",_allSoureArray.count);
            
            NSLog(@"dd = %@",dd);
            
            NSString *str = [NSString stringWithFormat:YOUJIJIAZAI_URL,dd];
            
            [[DisCoverYouJIManager shareDisCoverYouJIManager]requestNetWork:str];
            
        }else if (_disyoujiSegment.selectedSegmentIndex == 1){
        
        
        }else if (_disyoujiSegment.selectedSegmentIndex ==2){
        
        
            
            [[DisCoverYouJIManager shareDisCoverYouJIManager]requestXuanShangNetWork:[NSString stringWithFormat:XUANSHANG_URL,(NSInteger)_i]];
            
            NSLog(@"i ==== %d",_i);
            
            _i++;
        
        }
        
        
        
        
        
        if (_disyoujiSegment.selectedSegmentIndex == 0){
            
            // 结束刷新
            [self.youjicollectionView.mj_footer endRefreshing];
           
            
        }else if (_disyoujiSegment.selectedSegmentIndex == 1){
            
            
            
        }else if (_disyoujiSegment.selectedSegmentIndex == 2){
            
            
            // 结束刷新
            [self.xuanshangTableView.mj_footer endRefreshing];
            
        }
        
        
       
        
      
    });

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}



@end
